package com.wjm.demo.test1.net_test.tcp_socket_test.uploadpic;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class UploadPicServer {
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(10001);
        Socket accept = ss.accept();
        String ip = accept.getInetAddress().getHostAddress();
        System.out.println(ip + ".......connected");
        InputStream is = accept.getInputStream();
        File fileDir = new File("d:\\server");
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        File file = new File(fileDir, ip + "微信截图_20180707211416.png");
        FileOutputStream fos = new FileOutputStream(file);

        byte[] buf = new byte[1024];
        int len;
        while ((len = is.read(buf)) != -1) {
            fos.write(buf, 0, len);
        }
        OutputStream out = accept.getOutputStream();

        out.write("上传成功".getBytes());

        fos.close();

        accept.close();
        ss.close();
    }
}
